package io.zerous.engine.utils

/**
 * Created by Zerous on 2018/2/4.
 */

class Pool<T>(private val maxSize: Int, private val createObject: () -> T) {
    private val freeObjects: MutableList<T>

    init {
        freeObjects = ArrayList(maxSize)
    }

    fun newObject() = if (freeObjects.isEmpty()) {
        createObject()
    } else {
        freeObjects.removeAt(freeObjects.lastIndex)
    }

    fun free(obj: T) : Unit{
        if (freeObjects.size < maxSize)
            freeObjects.add(obj)
    }
}
